package com.personalsystem.account.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.commons.io.filefilter.FalseFileFilter;
import org.hibernate.type.TrueFalseType;

import net.sf.json.JSONObject;

import com.personalsystem.function.bean.Functions;
import com.personalsystem.tool.page.Page;

public class RoleUtil {
	
	public String getTreeJson(List<Functions> list,List<Long> lList){
		if(list==null || list.size()==0){
			return "";
		}
		Functions root=null;
		for(Functions function:list){
			if(function.getPid()==-1){
				root=function;
				break;
			}
		}
		if(root==null){
			return "";
		}
		Map map=createMap(root.getId(), list,lList);
		JSONObject jsonObject=new JSONObject();
		jsonObject.putAll(map);
		return jsonObject.toString();
	}
	
	
	public String getTableJson(Page page){
		Map map=new TreeMap();
		map.put("totalCount", page.getTotalCount());
		map.put("list", page.getList());
		JSONObject jsonObject=new JSONObject();
		jsonObject.putAll(map);
		return jsonObject.toString();
	}
	
	
	private Map createMap(long id,List<Functions> list,List<Long> lList){
		Map m=new TreeMap();
		for(Functions entity:list){
			if(entity.getId()==id){
				m.put("id", entity.getId()+"");
				m.put("text", entity.getName());
				m.put("url", entity.getUrl());
				if(lList!=null && lList.contains(entity.getId())){
					m.put("checked", true);
				}else{
					m.put("checked", false);
				}
				break;
			}
		}
		boolean isLeaf=true;
		List childList=new ArrayList();
		for(Functions entity:list){
			if(entity.getPid()==id){
				isLeaf=false;
				childList.add(createMap(entity.getId(), list,lList));
			}
		}
		if(isLeaf){
			m.put("leaf", true);
		}else{
			m.put("leaf", false);
			m.put("children", childList);
		}
		return m;
	}

}
